#ifndef __MENU_INC__
#define __MENU_INC__

// Pretty much like rotate_cover function in inc/playback.inc, but
// plays a bit more with the background images (moves them, etc..)
Process menu_rotate_cover(song pointer kar, int delay)
Private
  int id1=0, id2=0, time=0, i=0, j=10, v1x=0, v1y=0, v2x=0, v2y=0;
End
  
Begin
  graph=0;
  
  // Check the file exists
  if(kar.cover != "")
    if (! file_exists(kar.cover))
      kar.cover = iconv(kar.charset, "UTF8//TRANSLIT", kar.cover);
      if(! file_exists(kar.cover))
        kar.cover = datadir+"/img/menu_bg.png";
      end;
    end;
  else
    kar.cover = datadir+"/img/menu_bg.png";
  end;
  
  if(kar.bckgrnd != "")
    if (! file_exists(kar.bckgrnd))
      kar.bckgrnd = iconv(kar.charset, "UTF8//TRANSLIT", kar.bckgrnd);
      if(! file_exists(kar.bckgrnd))
        kar.bckgrnd = datadir+"/img/menu_bg.png";
      end;
    end;
  else
    kar.bckgrnd = datadir+"/img/menu_bg.png";
  end;

  dbg_print("DEBUG: Background: '"+kar.bckgrnd+"'");
  dbg_print("DEBUG: Cover:      '"+kar.cover+"'");
  
  //Create both robot processes, will guide them from this one
  id1 = robot();
  if(is_png(kar.cover))
    id1.graph = load_png(kar.cover);
  else
    id1.graph = load_image(kar.cover);
  end;
  id1.alpha=125;
  
  id2 = robot();
  if(is_png(kar.bckgrnd))
    id2.graph = load_png(kar.bckgrnd);
  else
    id2.graph = load_image(kar.bckgrnd);
  end;
  id2.alpha=0;
  
  //Resize the video so it fits the screen
  resize_fullscreen(id1);
  //And now make it MUCH bigger
  id1.size = 3*id1.size;
  //Position the video in the center of the window
  id1.x = resx/2;
  id1.y = resy/2;
  id1.z = 255;

  //Resize the video so it fits the screen
  resize_fullscreen(id2);
  //And now make it MUCH bigger
  id2.size = 3*id2.size;
  //Position the video in the center of the window
  id2.x = resx/2;
  id2.y = resy/2;
  id2.z = 255;

  v1x = rand(-3, 3); v1y = rand(-3,3);
  v2x = rand(-3, 3); v2y = rand(-3,3);

  //Now we're sure we've got both a cover and a BG image  
  time = timer[8];
  while(die == 0)
    if(timer[8] > time+delay)
      dbg_print("Rotating cover @ time="+timer[8]+"cs");
      for(i=0; i<=125/abs(j); i++)
        id1.alpha-=j;
        id2.alpha+=j;
        dbg_print("Fading "+i+" id1.alpha="+id1.alpha+" id2.alpha="+id2.alpha);
        FRAME;
      end;
      
      dbg_print("Finished fading");
      
      // Restart the time count & restart the alpha values & revert the
      // fade effect for the next time it is executed
      // Also, change the image's speed while it's transparent
      if (id1.alpha > 125) id1.alpha = 125; end;
      if (id2.alpha > 125) id2.alpha = 125; end;
      if (id1.alpha <= 0 )
        id1.alpha = 0;
        id1.x = resx/2;
        id1.y = resy/2;
        v1x = rand(-3, 3); v1y = rand(-3,3);
      end;
      if (id2.alpha <= 0 )
        id2.alpha = 0;
        id2.x = resx/2;
        id2.y = resy/2;
        v2x = rand(-3, 3); v2y = rand(-3,3);
      end; 
      time = timer[8];
      j = -j;
    end;
    
    // Move the images
    id1.x += v1x; id1.y += v1y;
    id2.x += v2x; id2.y += v2y;
    FRAME;
  end;
  
  // Tell the two processes to die, and unload their graphs
  id1.die = 1;
  if (id1.graph != 0)
    unload_map(0, id1.graph);
    id1.graph = 0;
  end;
  id2.die = 1;
  if(id2.graph != 0)
    unload_map(0, id2.graph);
    id2.graph = 0;
  end;
  return;
End;

// This process controls the main menu, and its movement.
Process menu()
Private
  int procid=0, audioid=0, i=0, j=0, announcer_pid=0;
  int carrousel_pid1=0, carrousel_pid2=0, carrousel_pid3=0;
  int carrousel_theta=0, vtheta=4000, carrousel_moving=0;
  song song_preview;
  
Begin
  // Create the processes for the carrousel
  carrousel_pid1 = robot();
  carrousel_pid2 = robot();
  carrousel_pid3 = robot();

  // Clear the background
  clear_screen();
  
  // Place the elements on the carrousel
  // carrousel_pid1.aux defines the offset angle
  carrousel_pid1.aux =  60000.0;
  carrousel_pid3.aux = -60000.0;
  carrousel_pid2.aux =      0.0;

  carrousel_pid1.x = resx/2+resx*sin(carrousel_theta+carrousel_pid1.aux)/3.5;
  carrousel_pid1.y = resy/2+50.0*cos(carrousel_theta+carrousel_pid1.aux);
  carrousel_pid1.z = 0;
  carrousel_pid1.size = 100-abs(carrousel_theta+carrousel_pid1.aux)/1000.0;
  carrousel_pid1.alpha = 255*(1.0-0.4*abs(carrousel_theta+carrousel_pid1.aux)/90000.0);
  carrousel_pid1.graph = songs[1].cover;
  carrousel_pid3.x = resx/2+resx*sin(carrousel_theta+carrousel_pid3.aux)/3.5;
  carrousel_pid3.y = resy/2+50.0*cos(carrousel_theta+carrousel_pid3.aux);
  carrousel_pid3.z = 0;
  carrousel_pid3.size = 100-abs(carrousel_theta+carrousel_pid3.aux)/1000.0;
  carrousel_pid3.alpha = 255*(1.0-0.4*abs(carrousel_theta+carrousel_pid3.aux)/90000.0);
  carrousel_pid3.graph = songs[songs_found-1].cover;
  carrousel_pid2.x = resx/2+resx*sin(carrousel_theta+carrousel_pid2.aux)/3.5;;
  carrousel_pid2.y = resy/2+50.0*cos(carrousel_theta+carrousel_pid2.aux);
  carrousel_pid2.z = -1;
  carrousel_pid2.size = 100-abs(carrousel_theta+carrousel_pid2.aux)/1000.0;
  carrousel_pid2.alpha = 255*(1.0-0.4*abs(carrousel_theta+carrousel_pid2.aux)/90000.0);
  carrousel_pid2.graph = songs[0].cover;
  
  // Make the covers change in the background, and play it
  song_preview.cover = songs[i].bgpath;
  song_preview.bckgrnd = songs[i].coverpath;
  if(announcer_pid != 0)
    announcer_pid.die = 1;
    FRAME;      // We allow the background song announcer to die gracefully
  End;
  announcer_pid = menu_rotate_cover(&song_preview, 600);
  // If music is being played, stop it
  if(audioid != 0)
    fmodex_stop_song();
	fmodex_unload_song();
  end;
  fmodex_load_song(songs[i].music_file);
  fmodex_play_song();
  audioid=1;

  timer[9] = 0;
  LOOP
    // This code handles carrousel movement
    if(carrousel_moving != 0)
      if(abs(carrousel_theta) < 60000)
        carrousel_theta += carrousel_moving*vtheta;

        carrousel_pid1.x = resx/2+resx*sin(carrousel_theta+carrousel_pid1.aux)/3.5;
        carrousel_pid1.y = resy/2+50.0*cos(carrousel_theta+carrousel_pid1.aux);
        carrousel_pid1.size = 100-abs(carrousel_theta+carrousel_pid1.aux)/1000.0;
        carrousel_pid1.alpha = 255*(1.0-0.4*abs(carrousel_theta+carrousel_pid1.aux)/90000.0);
        carrousel_pid3.x = resx/2+resx*sin(carrousel_theta+carrousel_pid3.aux)/3.5;
        carrousel_pid3.y = resy/2+50.0*cos(carrousel_theta+carrousel_pid3.aux);
        carrousel_pid3.size = 100-abs(carrousel_theta+carrousel_pid3.aux)/1000.0;
        carrousel_pid3.alpha = 255*(1.0-0.4*abs(carrousel_theta+carrousel_pid3.aux)/90000.0);
        carrousel_pid2.x = resx/2+resx*sin(carrousel_theta+carrousel_pid2.aux)/3.5;;
        carrousel_pid2.y = resy/2+50.0*cos(carrousel_theta+carrousel_pid2.aux);
        carrousel_pid2.size = 100-abs(carrousel_theta+carrousel_pid2.aux)/1000.0;
        carrousel_pid2.alpha = 255*(1.0-0.4*abs(carrousel_theta+carrousel_pid2.aux)/90000.0);

        // Change the z of the covers, when they cross their ways
        if( (carrousel_pid2.size < carrousel_pid1.size) && carrousel_moving < 0)
          carrousel_pid2.z = 0;
          carrousel_pid1.z = -1;
        end;
        
        if((carrousel_pid2.size < carrousel_pid3.size) && carrousel_moving > 0)
          carrousel_pid2.z = 0;
          carrousel_pid3.z = -1;
        end;

        // Check if we must replace the cover (because it became so small
        // that it's no longer visible) and then place it on the other
        // side of the carrousel
        if(carrousel_pid3.size <= 15)
          carrousel_pid3.aux = 120000.0;
          carrousel_pid3.graph = songs[i+1].cover;
        end;
        
        if(carrousel_pid1.size <= 15)
          carrousel_pid1.aux = -120000.0;
          carrousel_pid1.graph = songs[i-1].cover;
        end;
      else
        // The carrousel was moving, but must now stop, as it's reached the end
        carrousel_moving = 0;
        carrousel_theta = 0.0;
        carrousel_pid1.aux =  60000.0;
        carrousel_pid3.aux = -60000.0;
        carrousel_pid2.aux =      0.0;

        carrousel_pid1.x = resx/2+resx*sin(carrousel_theta+carrousel_pid1.aux)/3.5;
        carrousel_pid1.y = resy/2+50.0*cos(carrousel_theta+carrousel_pid1.aux);
        carrousel_pid1.z = 0;
        carrousel_pid1.size = 100-abs(carrousel_theta+carrousel_pid1.aux)/1000.0;
        carrousel_pid1.alpha = 255*(1.0-0.4*abs(carrousel_theta+carrousel_pid1.aux)/90000.0);
        if(i > songs_found-2)
          carrousel_pid1.graph = songs[0].cover;
        else
          carrousel_pid1.graph = songs[i+1].cover;
        end;
        carrousel_pid3.x = resx/2+resx*sin(carrousel_theta+carrousel_pid3.aux)/3.5;
        carrousel_pid3.y = resy/2+50.0*cos(carrousel_theta+carrousel_pid3.aux);
        carrousel_pid3.z = 0;
        carrousel_pid3.size = 100-abs(carrousel_theta+carrousel_pid3.aux)/1000.0;
        carrousel_pid3.alpha = 255*(1.0-0.4*abs(carrousel_theta+carrousel_pid3.aux)/90000.0);
        if(i > 0)
          carrousel_pid3.graph = songs[i-1].cover;
        else
          carrousel_pid3.graph = songs[songs_found-1].cover;
        end;
        carrousel_pid2.x = resx/2+resx*sin(carrousel_theta+carrousel_pid2.aux)/3.5;;
        carrousel_pid2.y = resy/2+50.0*cos(carrousel_theta+carrousel_pid2.aux);
        carrousel_pid2.z = -1;
        carrousel_pid2.size = 100-abs(carrousel_theta+carrousel_pid2.aux)/1000.0;
        carrousel_pid2.alpha = 255*(1.0-0.4*abs(carrousel_theta+carrousel_pid2.aux)/90000.0);
        carrousel_pid2.graph = songs[i].cover;

        // Make the covers change in the background, and play it
        song_preview.cover = songs[i].bgpath;
        song_preview.bckgrnd = songs[i].coverpath;
        if(announcer_pid != 0)
          announcer_pid.die = 1;
          FRAME;      // We allow the background song announcer to die gracefully
        End;
        announcer_pid = menu_rotate_cover(&song_preview, 600);
        // If music is being played, stop it
        if(audioid != 0)
          fmodex_stop_song();
		  fmodex_unload_song();
		  audioid=0;
        end;
        fmodex_load_song(songs[i].music_file);
		fmodex_play_song();
		audioid=1;

      end;
    end;

    // Handle key input
    if(key(_right) && carrousel_moving == 0)
      i++;
      if(i >= songs_found)
        i=0;
      end;
      
      // Put the cover on the right on the bottom
      carrousel_pid1.z++;
      carrousel_moving = -1;
    end;
    
    if(key(_left) &&  carrousel_moving == 0)
      i--;
      if(i < 0)
        i=songs_found-1;
      end;
      
      // Put the cover on the right on the bottom
      carrousel_pid3.z++;
      carrousel_moving = 1;
    end;
    
    // Start playing the song
    if( key(_enter) && carrousel_moving == 0)
      //Fade off
      fade(0, 0, 0, 15);
      while(fading)
        FRAME;
      end;

      clear_screen();
      graph=0;
      delete_text(all_text);
      // Tell the carrousel to die
      if(exists(carrousel_pid1))
        carrousel_pid1.die = 1;
      end;
      if(exists(carrousel_pid2))
        carrousel_pid2.die = 1;
      end;
      if(exists(carrousel_pid3))
        carrousel_pid3.die = 1;
      end;
      FRAME;

      // Tell the announcer to die, if still on screen
      if(exists(announcer_pid))
        announcer_pid.die = 1;
      end;
      
      // If music is being played, stop it
      if(audioid != 0)
        fmodex_stop_song();
		fmodex_unload_song();
		audioid=0;
      end;
      
      // Start the process that will play the song
      procid = play_file(songs[i].dirname, songs[i].filename);
      if(procid == -1)		// Failed :(
        dbg_print("Couldn't play specified file, aborting!");
        let_me_alone();
        menu();
        //Fade back on to the new menu, then die
        fade(100, 100, 100, 15);
        while(fading)
          FRAME;
        end;
        return -1;
      end;
      
      //Fade back on to the song
      fade(100, 100, 100, 15);
      while(fading)
        FRAME;
      end;

      return;     //Play_file should create a new menu for us
    end;
    
    // Quit from the menu
    if(key(_esc) && carrousel_moving == 0)
      //Fade off
      fade(0, 0, 0, 15);
      while(fading)
        FRAME;
      end;
      // Unset the graphic
      graph = 0;
      
      // Tell the carrousel to die
      if(exists(carrousel_pid1))
        carrousel_pid1.die = 1;
      end;
      if(exists(carrousel_pid2))
        carrousel_pid2.die = 1;
      end;
      if(exists(carrousel_pid3))
        carrousel_pid3.die = 1;
      end;
      FRAME;    // This way, they'll die gracefully
      
      // Unload all the cover art
      for(i=0; i < MAX_ALLOWED_SONGS; i++)
        if(songs[i].cover != 0)
          unload_map(0, songs[i].cover);
        end;
      end;
      
      exit();
    end;
    FRAME;
  end;
End;

// This process implements the menu used when no songs is found
Process no_songs_menu()
Private
  int pid1=0, pid2=0, pid2_graph_noglow=0, pid2_graph_glow=0, aux_x=0, aux_y=0;
  string songsdir="";

Begin
  //Display the error
  pid1 = robot();
  pid1.graph = load_png(datadir+"img/no_songs_found.png");
  pid1.x = resx/2; pid1.y = resy/2; pid1.z = 255;
  resize_fullscreen(pid1);
  
  //Show the link to the songs folder, place it correctly onscreen
  pid2 = robot();
  pid2.graph = pid2_graph_noglow = load_png(datadir+"img/songs_folder_noglow.png");
  pid2_graph_glow = load_png(datadir+"img/songs_folder_glow.png");
  pid2.size = pid1.size;
  aux_x = graphic_info(0, pid1.graph, G_WIDTH);
  aux_y = graphic_info(0, pid1.graph, G_HEIGHT);
  pid2.x = pid1.x-0.36*aux_x*(pid1.size/100.0);
  pid2.y = pid1.y+0.25*aux_y*(pid1.size/100.0);
  
  // We'll use the width & height of pid2 to compute collisions with the mouse
  aux_x = graphic_info(0, pid2.graph, G_WIDTH);
  aux_y = graphic_info(0, pid2.graph, G_HEIGHT);
  
  // Enable the mouse
  mouse_controller();
  
  while( die == 0 )
    if( key(_enter) || key(_esc) || key(_space))
      die = 1;
    end;

    // Check if we should change the graph for the link
    if( mouse.x >= (pid2.x-aux_x/2) && mouse.x <= (pid2.x+aux_x/2) &&
        mouse.y >= (pid2.y-aux_y/2) && mouse.y <= (pid2.y+aux_y/2) )
        
        // If the user clicked the mouse, show the songs folder, then quit.
        if(mouse.left)
          songsdir = datadir + "/Songs/";
          say("Opening songs dir: "+songsdir);
          create_dir_hierarchy(songsdir);
          exec(_P_NOWAIT, "xdg-open", 1, &songsdir);
          exit();
        end;
        
        pid2.graph = pid2_graph_glow;
    else
      pid2.graph = pid2_graph_noglow;
    end;
    FRAME;
  end;
  //Unload the graph of the robot, tell it to die
  unload_map(0, pid1.graph);
  pid1.graph=0;
  pid1.die=1;
  
  pid2.graph = 0;
  pid2.die = 1;
  unload_map(0, pid2_graph_glow); unload_map(0, pid2_graph_noglow);
  
  //Now quit
  exit();
End;

#endif
